package com.ruoyi.web.controller.personal;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.domain.SalaryReduction;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.web.controller.vo.UserDeptVO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SalaryStandard;
import com.ruoyi.system.service.ISalaryStandardService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 薪资标准Controller
 * 
 * @author ruoyi
 * @date 2023-11-15
 */
@RestController
@RequestMapping("/system/standard")
public class SalaryStandardController extends BaseController
{
    @Autowired
    private ISalaryStandardService salaryStandardService;


    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ISysDeptService sysDeptService;

    @Anonymous
    @GetMapping("/userList")
    public AjaxResult getUserList(){
        List<SysUser> sysUsers = sysUserService.getAllUsers();

//        List<SysUser> sysUsers = sysUserService.selectUserList(new SysUser());
        List<UserDeptVO> userDeptVOList = new ArrayList<>();
        for(SysUser user : sysUsers){
            SysDept sysDept = sysDeptService.selectDeptById(user.getDeptId());
            UserDeptVO userDeptVO = new UserDeptVO(user.getUserId(), user.getUserName(), user.getDeptId(), sysDept.getDeptName());
            userDeptVOList.add(userDeptVO);

        }
        return AjaxResult.success(userDeptVOList);
    }

    @Anonymous
    @GetMapping("/standardId/{id}")
    public AjaxResult getSalaryReductionListBySalaryStandardId(@PathVariable("id") Long id){

        List<SalaryReduction> list = salaryStandardService.selectSalaryReductionListBySalaryStandardId(id);
        return AjaxResult.success(list);
    }

    /**
     * 查询薪资标准列表
     */
    @PreAuthorize("@ss.hasPermi('system:standard:list')")
    @GetMapping("/list")
    public TableDataInfo list(SalaryStandard salaryStandard)
    {
        startPage();
        List<SalaryStandard> list = salaryStandardService.selectSalaryStandardList(salaryStandard);
        return getDataTable(list);
    }

    /**
     * 导出薪资标准列表
     */
    @PreAuthorize("@ss.hasPermi('system:standard:export')")
    @Log(title = "薪资标准", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SalaryStandard salaryStandard)
    {
        List<SalaryStandard> list = salaryStandardService.selectSalaryStandardList(salaryStandard);
        ExcelUtil<SalaryStandard> util = new ExcelUtil<SalaryStandard>(SalaryStandard.class);
        util.exportExcel(response, list, "薪资标准数据");
    }

    /**
     * 获取薪资标准详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:standard:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(salaryStandardService.selectSalaryStandardById(id));
    }

    /**
     * 新增薪资标准
     */
    @PreAuthorize("@ss.hasPermi('system:standard:add')")
    @Log(title = "薪资标准", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SalaryStandard salaryStandard)
    {
        return toAjax(salaryStandardService.insertSalaryStandard(salaryStandard));
    }

    /**
     * 修改薪资标准
     */
    @PreAuthorize("@ss.hasPermi('system:standard:edit')")
    @Log(title = "薪资标准", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SalaryStandard salaryStandard)
    {
        return toAjax(salaryStandardService.updateSalaryStandard(salaryStandard));
    }

    /**
     * 删除薪资标准
     */
    @PreAuthorize("@ss.hasPermi('system:standard:remove')")
    @Log(title = "薪资标准", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(salaryStandardService.deleteSalaryStandardByIds(ids));
    }
}
